const app = getApp()      //获取小程序实例
import { add, count, get, getIn } from "../../utils/db";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLogin: false, //是否登录。 false 未登录  true，已经登录
    userInfo: null,
    activeIndex: 0,    //tab栏默认下标
    menuList: [],      //菜谱列表
    likeList: [],        //收藏列表
    types: [],    //分类列表

  },
  onLoad() {
    //获取用户公开信息
    this.getUserInfo()

    //获取我发布的菜谱
    this.getMenuList()

    //获取我关注的菜谱
    this.getMyLikes()

     //获取我分类的菜谱
     this.getList()
  },
   //获取type列表
   async getList() {
    let openid = wx.getStorageSync('openid')
    let result = await get({ collection: "type", where: { _openid: openid } }).catch(err => {
      console.log(err)
    })
    // console.log(result.data)
    this.setData({
      types: result.data
    })
  },
  //获取我关注的菜谱
  async getMyLikes() {
    let openid = wx.getStorageSync("openid");
    let result = await get({
      collection: "likes",
      where: { _openid: openid }
    }).catch(err => {
      console.error(err)
    })
    // 我关注的菜谱id，所组成的字符串数组   
    let arr = result.data.map(item => {
      return item.menuid
    })

    let res = await getIn({ collection: "menu", condition: arr, field: "_id" }).catch(err => {
      console.error(err)
    })
    // console.log(res)


    res.data.forEach(item => {
      //计算星星
      if (item.views >= 0 && item.views <= 10) {
        item.star = 1
      } else if (item.views > 10 && item.views <= 20) {
        item.star = 2
      } else if (item.views > 20 && item.views <= 30) {
        item.star = 3
      } else if (item.views > 30 && item.views <= 40) {
        item.star = 4
      } else {
        item.star = 5
      }
    })
    //渲染到页面
    this.setData({
      likeList: res.data
    })
  },
  //获取我发布的菜谱
  async getMenuList() {
    let openid = wx.getStorageSync('openid');
    let result = await get({ collection: "menu", where: { _openid: openid } }).catch(err => {
      console.log(err)
    })
    // console.log(result)
    this.setData({
      menuList: result.data
    })
  },
  //获取用户信息
  getUserInfo() {
    //从全局变量中获取userInfo
    let userInfo = app.globalData.userInfo;
    if (userInfo == null) {
      //定义函数
      app.getInfo = (res) => {
        this.setData({
          userInfo: res.userInfo,
          isLogin: true
        })

      }
    } else {
      this.setData({
        userInfo,
        isLogin: true
      })
    }
  },

  //获取用户公开信息
  async getInfo(e) {
    let userInfo = e.detail.userInfo;   //用户的公开信息
    //检查数据库中，用户是否存在。如果不存在，则添加用户
    let openid = wx.getStorageSync('openid');

    // let res=await db.collection("users").where({_openid:openid}).count()
    let res = await count({ collection: "users", where: { _openid: openid } }).catch(err => {
      console.error(err)
    })

    if (res.total == 0) {
      //添加数据库
      let result = await add({
        collection: "users", data: {
          addtime: new Date().getTime(),
          ...userInfo
        }
      }).catch(err => { console.error(err) })

    }

    //渲染到页面

    this.setData({
      userInfo,
      isLogin: true
    })
  },
  //点击tab
  tabClick(e) {
    // console.log(e)
    let activeIndex = e.currentTarget.id;
    this.setData({
      activeIndex
    })
  },
  //跳转到发布菜谱页面
  toAdd() {
    console.log(1111)
    wx.navigateTo({
      url: '/pages/pbrecipe/pbrecipe',
      success: res => {
        console.log(res)
      },
      fail: err => {
        console.log(err, "333")
      }
    })
  },
  //跳转到详情页
  toDetail(e) {
    // let id=e.id;      //菜谱的id
    let id = e.currentTarget.id;    //菜谱id
    wx.navigateTo({
      url: '/pages/detail/detail?id=' + id,
    })
  },
  //跳转到type页面
  toType(){
    wx.navigateTo({
      url: '/pages/type/type' ,
    })
  }

})